#include <stdio.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"

#include "esp_log.h"

#include "driver/temperature_sensor.h"

#include "lvgl_gui.h"
#include "lv_demos/lv_demo.h"

TaskHandle_t xMainTask;
SemaphoreHandle_t xGuiSemaphore;

static const char *TAG = "lvgl benchmark";

static void dump_haps()
{

    float tsens_out;
    temperature_sensor_config_t temp_sensor = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
    temperature_sensor_handle_t temp_handle = NULL;
    ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor, &temp_handle));
    ESP_ERROR_CHECK(temperature_sensor_start(temp_handle));
    ESP_LOGI(TAG, "Temperature sensor started");

    while (1)
    {
        size_t size = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
        size_t size_internal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
        ESP_LOGW(TAG, "%d, %d", size, size_internal);

        ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
        ESP_LOGI(TAG, "Temperature out celsius %.02f", tsens_out);

        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void app_main(void)
{
    xMainTask = xTaskGetCurrentTaskHandle();

    StaticTask_t xTaskBuffer;
    StackType_t *xStack = heap_caps_calloc(1024 * 32, sizeof(StackType_t), MALLOC_CAP_SPIRAM);

    xTaskCreateStaticPinnedToCore(guiTask, "gui", 1024 * 32, NULL, 5, xStack, &xTaskBuffer, 1);
    ulTaskNotifyTakeIndexed(0, pdTRUE, portMAX_DELAY);

    // xTaskCreate(dump_haps, "dump_haps", 1024 * 4, NULL, 5, NULL);
}
